package cate.game.role.bag.equip;

import cate.common.table.item.gem.row.GemBaseRow;
import cate.game.attr.GemPowerAttrPart;
import cate.game.attr.PowerAttrPart;
import cate.game.play.support.PlayBuildContext;
import cate.game.role.Role;
import cate.game.role.RoleDependent;
import cate.game.role.bag.BagContainer;
import cate.game.role.bag.gem.Gem;
import cate.game.role.bag.hero.Hero;
import cate.game.role.bag.hero.OnHeroItem;
import cate.game.util.GameKit;
import com.fasterxml.jackson.annotation.JsonIgnore;
import cp.solution.util.collection.MapKit;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;
import org.apache.commons.lang3.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

@NoteClass(value = "装备上的宝石")
public class GemsOn extends RoleDependent implements OnHeroItem<Gem> {
    @NoteField(value = "宝石", detail = "Map<部位:Byte, 宝石信息>")
    public Map<Byte, Gem> gemMap;

    public GemsOn(){

    }

    @Override
    public void initialize(Role role){
        super.initialize(role);
        if(gemMap == null){
            gemMap = new HashMap<>();
        }
        gemMap.values().forEach( e -> {
            if (e != null) {
                e.initialize(role);
            }
        });
    }

    @Override
    @JsonIgnore
    public BagContainer<Gem> getItemBag() {
        return role.getBag().gem;
    }

    @Override
    @JsonIgnore
    public Map<Byte, Gem> getPosItems() {
        return gemMap;
    }

    @Override
    @JsonIgnore
    public Gem getPosItem(byte pos){
        return gemMap.get(pos);
    }

    @Override
    public void clearPosItems(){
        gemMap.clear();
    }

    @Override
    public void doRealOn(byte pos, Gem gem) {
        // 穿上装备就还uid
        GemBaseRow gemBaseRow = gem.getTpl(role.getGame().fight.getPlayBuildContext());
        if (gemBaseRow == null || gemBaseRow.unique()) {
            gemMap.put(pos, gem);
        } else {
            Gem tempGem = gem.copy();
            tempGem.uid = GameKit.uid();
            tempGem.num = 1;
            gemMap.put(pos, tempGem);
        }
    }

    @Override
    public void doRealOff(byte pos, Gem gem) {
        gemMap.remove(pos);
    }

    @Override
    @Deprecated
    @JsonIgnore
    public PowerAttrPart getPowerAttrPart(PlayBuildContext ctx) {
        return null;
    }

    @JsonIgnore
    public PowerAttrPart getPowerAttrPart(PlayBuildContext ctx, Hero owner) {
        return new GemPowerAttrPart(ctx, this, owner);
    }

    @JsonIgnore
    public List<Integer> getSkills(PlayBuildContext ctx, Hero owner){
        return gemMap.values().stream()
                .flatMap(e -> e.getSkills(ctx, owner).stream())
                .collect(Collectors.toList());
    }

    @Override
    public GemsOn copy(){
        GemsOn cp = new GemsOn();
        cp.gemMap = MapKit.cloneDeep(this.gemMap);
        cp.initialize(this.role);
        return cp;
    }

    public Gem getGem(String gemUid) {
        return gemMap.values().stream().filter(e -> StringUtils.equals(e.uid, gemUid)).findAny().orElse(null);
    }
}
